package com.apes.autoparts.supplier.platform;

import com.apes.framework.api.SpringManager;
import com.apes.framework.plugin.cache.MemoryCache;

import java.util.HashMap;
import java.util.Map;

public class SupplierPlatformUtil {
    public static <T extends SupplierPlatformSearch> T getBusinessExecutor(Class<T> clazz, String source) {
        String key = "supplierPlatform.executor." + clazz.getSimpleName();
        Map cache = (Map) MemoryCache.getInstance().get(key);
        if (cache == null) {
            synchronized (SupplierPlatformUtil.class) {
                cache = new HashMap<>();
                for (T executor : SpringManager.getBeansOfType(clazz).values()) {
                    cache.put(executor.getSource(), executor);
                }
                MemoryCache.getInstance().put(key, cache);
            }
        }
        return (T) cache.get(source);
    }
}
